package com.bskj.sj.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bskj.common.bean.model.AggregateAmountsVo;
import com.bskj.common.bean.model.FindSumChargeVo;
import com.bskj.common.bean.model.ParkInfo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.constants.ReturnCodeMsgConstants;
import com.bskj.common.dao.ParkInfoDao;
import com.bskj.common.dao.ParkingLotCustomerDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.ParkChargeTypeEnum;
import com.bskj.common.utils.StringUtils;
import com.bskj.sj.bean.vo.resp.ParkTransDetailRespVo;
import com.bskj.sj.bean.vo.resp.ParkTransTotalRespVo;
import com.bskj.sj.service.IParkInfoService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.sj.service.IParkingLotCustomerService;

@Service
public class ParkingLotCustomerServiceImpl extends BaseServiceImpl<ParkingLotCustomer> implements
        IParkingLotCustomerService {
    @Autowired
    private ParkingLotCustomerDao parkingLotCustomerDao;

    @Autowired
    private IParkInfoService parkInfoService;
    
    @Autowired
    private ParkInfoDao parkInfoDao;
    
    public BaseDao<ParkingLotCustomer> getBaseDao() {
        return parkingLotCustomerDao;
    }
    
    public Map<String, String> validParkLot(Integer parkLotId, String parkLotUuid) throws Exception {
        Map<String, String> resultMap = new HashMap<String, String>();
        ParkingLotCustomer parkingLot = get(parkLotId);
        if (parkingLot == null) {
            resultMap.put("resultCode", ReturnCodeMsgConstants.REQUEST_INVALID);
            resultMap.put("resultMsg", "停车场不存在!");
            return resultMap;
        } else {
            if (!parkingLot.getUuid().equals(parkLotUuid)) {
                resultMap.put("resultCode", ReturnCodeMsgConstants.REQUEST_INVALID);
                resultMap.put("resultMsg", "停车场信息不合法!");
                return resultMap;
            }
        }
        return resultMap;
    }

    @Override
    public ParkTransTotalRespVo getTransTotal(FindSumChargeVo findSumChargeVo) throws Exception {
    	if(findSumChargeVo.getStartTime()==null){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		findSumChargeVo.setStartTime(sdf.format(new Date())+" 00:00:00");
    		findSumChargeVo.setEndTime(sdf.format(new Date())+" 23:59:59");
    	}
        ParkTransTotalRespVo parkTransTotalRespVo = new ParkTransTotalRespVo();
        findSumChargeVo.setChargeType(ParkChargeTypeEnum.CASH_PAY.getKey());
        List<AggregateAmountsVo> aggregateCash=parkInfoDao.getSumCharge(findSumChargeVo);
        if (aggregateCash!=null&&aggregateCash.size()==1) {
			parkTransTotalRespVo.setCashCount(aggregateCash.get(0).getCount());
			parkTransTotalRespVo.setCashAmount(aggregateCash.get(0).getSum());
		}else {
			parkTransTotalRespVo.setCashCount(0);
			parkTransTotalRespVo.setCashAmount(0);
		}
        findSumChargeVo.setChargeType(ParkChargeTypeEnum.PLATFROM_PAY.getKey());
        List<AggregateAmountsVo> aggregatePlatfrom=parkInfoDao.getSumCharge(findSumChargeVo);
        if(aggregatePlatfrom!=null&&aggregatePlatfrom.size()==1){
        	parkTransTotalRespVo.setPlatformCount(aggregatePlatfrom.get(0).getCount());
        	parkTransTotalRespVo.setPlatformAmount(aggregatePlatfrom.get(0).getSum());
        }else {
        	parkTransTotalRespVo.setPlatformCount(0);
        	parkTransTotalRespVo.setPlatformAmount(0);
		}
        parkTransTotalRespVo.setCustId(findSumChargeVo.getCustId());
        return parkTransTotalRespVo;
    }
    
    @Override
    public ParkTransDetailRespVo getTransDetail(Integer custId, String date, Integer page, Integer pageSize) throws Exception {
        if (page == null || page == 0) {
            page = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonConstatns.DEFAULT_PAGE_SZIE;
        }

        ParkTransDetailRespVo parkTransDetailRespVo = new ParkTransDetailRespVo();
        List<ParkInfo> cashList = getTransDetail(custId, date, 1, page, pageSize);// parkInfoService.find(findRequestEntity);
        parkTransDetailRespVo.setCashList(cashList);
        List<ParkInfo> platformList = getTransDetail(custId, date, 2, page, pageSize);
        parkTransDetailRespVo.setPlatformList(platformList);

        return parkTransDetailRespVo;
    }

    @Override
    public List<ParkInfo> getTransDetail(Integer custId, String date, int chargeType, Integer page, Integer pageSize) throws Exception {
        if (page == null || page == 0) {
            page = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonConstatns.DEFAULT_PAGE_SZIE;
        }
        String outTimeBegin = "";
        String outTimeEnd = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBank(date)) {
            outTimeBegin = simpleDateFormat.format(new Date());
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(new Date(), 1));
        } else {
            Date currentDate = simpleDateFormat.parse(date);
            outTimeBegin = date;
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(currentDate, 1));
        }

        FindRequestEntity findRequestEntity = new FindRequestEntity();
        findRequestEntity.addWhere("park_lot_custimer_id", WhereEnum.EQ, custId);
        findRequestEntity.addBetween("out_time", outTimeBegin, outTimeEnd);
        findRequestEntity.addOrder("create_time", OrderEnum.DESC);
        findRequestEntity.setPage(page, pageSize);
        //收费方式（0-未支付 1-现金 2-平台代扣）
        findRequestEntity.addWhere("charge_type", WhereEnum.EQ, chargeType);
        return parkInfoService.find(findRequestEntity);
    }

	@Override
	public List<ParkInfo> getTransDetail(Integer custId, int chargeType) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("park_lot_custimer_id", WhereEnum.EQ, custId);
		findReq.addWhere("charge_type", WhereEnum.EQ, chargeType);
		findReq.addOrder("out_time",OrderEnum.DESC);
		findReq.addWhere("charge", WhereEnum.GT, 0);
		findReq.setPage(1,20);
		List<ParkInfo> parkInfos=parkInfoDao.find(findReq);
		return parkInfos;
	}
}
